/**
 * @file    pcs_can_mng.c
 * @brief 
 * @author  Liuwei
 * @version 1.0.2
 * @date    2023-06-13
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-06-13 <td>1.0.1     <td>name     <td>首次创建
 * <tr><td>2024-01-03 <td>1.0.2     <td>Liuwei   <td>修改CAN波特率与协议关联
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "pcs_can_mng.h"
#include "param_mng.h"
#include "goodwe_can.h"
#include "growatt_can.h"
#include "pylon_can.h"
#include "sofar_can.h"
#include "victron_can.h"
#include "var_mng.h"
#include "deye_can.h"
#include "lxp_can.h"
#include "VMII_can.h"
#include "sma_can.h"
#include "invt_can.h"
#include "srne_can.h"
#include "iap_boot.h"
#include "boot_param.h"
#include "aiswei_can.h"
#include "must_can.h"
#include "cfg_prj.h"
#include "FreeRTOS.h"
#include "task.h"
#include "kstar_can.h"
/* macro/define --------------------------------------------------------------*/
#define CAN_TX_BUF_SIZE     (30)
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  PCS CAN通信管理
 * @param  can_id
 * @param  cycle
 */
void can_pcs_comm_mmg(uint8_t can_id , uint32_t cycle)
{
    uint8_t pcs_name = 0;
    uint16_t send_num = 0;
    uint16_t recv_num = 0;
    uint8_t i = 0;
	can_frame_type can_send_buf[CAN_TX_BUF_SIZE] = {0};
    can_frame_type can_recv_buf[20] = {0};
	
	if(can_id > CAN_ID1)
		return ;
	
    pcs_name = param_get_data(PAR_ID_PCS_CAN_TYPE);
	
	recv_num = If_can_read_one_frame(can_id , can_recv_buf);
	// 外部有CAN通信报文，清除通信超时状态 
	if(recv_num != 0)
	{
		var_set_data(VAR_ID_COMM_OFF_CNT , 0);
		var_set_data(VAR_ID_COMM_STATE , 0);
	}
	
	for( i = 0; i < recv_num; ++ i)
    {
		if(TRUE == is_iap_can_upgrade_cmd(can_recv_buf[0]))
		{
			iap_boot_jump_before_deal(BOOT_UPGRADE_CAN2);
		}
	}
		
    switch(pcs_name)
    {
        case PCS_CAN_PYLON:    /* (00) 派能协议 */
            send_num = pylon_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
        case PCS_CAN_GROWATT:  /* (01) 古瑞瓦特协议 */
            send_num = growatt_can_deal_msg(cycle , can_send_buf, recv_num , can_recv_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
        case PCS_CAN_GOODWE:   /* (02) 固德威协议 */
            send_num = goodwe_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
        case PCS_CAN_SOFAR:    /* (03) 首行协议 */
            send_num = sofar_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
        case PCS_CAN_VICTRON:  /* (04) 维克创协议 */
            send_num = victron_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
        case PCS_CAN_KSTAR: /* (05) 科士达 */
			send_num = kstar_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
		case PCS_CAN_LXP:  /* (06) 鹏程 */
            send_num = lxp_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
		case PCS_CAN_DEYE: /* (07) 德业 */
            send_num = deye_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
		case PCS_CAN_GINLONG: /* (08) 锦浪 */
            send_num = pylon_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
		case PCS_CAN_SMA:     /* (09) 艾斯玛 */
            send_num = sma_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
		case PCS_CAN_VMII:    /* (10) 南京欧陆 */
            send_num = VMII_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
		case PCS_CAN_SRNE:   /* (11) 硕日 */
            send_num = srne_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
        case PCS_CAN_INVENT: /* (12) 英威腾 */
            send_num = invt_can_deal_msg(cycle , can_send_buf, recv_num , can_recv_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
		case PCS_CAN_SOROUPS:  /* (13) 索瑞德 */
			send_num = sofar_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
		case PCS_CAN_MUST:  /* (14) 美世乐 */
			send_num = must_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
		case PCS_CAN_AISWEI:  /* (15) 爱士维 */
			 send_num = aiswei_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
			break;
        default:  // 默认派能 
            send_num = pylon_can_get_msg(cycle , can_send_buf);
            for( i = 0; (i < send_num) &&(i < CAN_TX_BUF_SIZE); ++ i)
            {
                If_can_send_frame(can_id , can_send_buf[i]);
				vTaskDelay(20);
            }
            break;
    }
}

/**
 * @brief  PCS 读取CAN逆变器波特率
 * @param  void
 * @param  void
 */
uint32_t pcs_can_get_com_rate(void)
{
	uint16_t pcs_rate = 0;
	
	pcs_rate = param_get_data(PAR_ID_PCS_CAN_TYPE);
	
	if(pcs_rate != PCS_CAN_MUST)
	{
		return 500000;
	}
	else
	{
		return 100000;
	}
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
